var WAITERS, SEARCH;
var baiduLocation;
// 获取附近的服务员
var getWaiters = function (callback) {
    // 获取位置信息
    baiduLocation.getLocation(
        function(ret, err){
            if(ret.status){
                var lat = ret.latitude;
                var lng = ret.longitude;
                var param = 'lat=' + lat + '&' + 'lng=' + lng;

                for (var key in SEARCH) {
                    var values = SEARCH[key];
                    if (values && values.length) {
                        values.some(function(value, i) {
                            param += '&' + key + '=' + value;
                        });
                    }
                }
                api.ajax({
            		url : SERVER_URL + '/entity/waiter?' + param,
            		method : 'get',
            		timeout: 30,
            		dataType : 'json',
            		cache : false
            	}, callback);
            } else{
                api.alert({msg: '服务端错误。' + err.msg});
            }
        }
    );
};
// 获取配置信息
var getConfigs = function (callback) {
    api.ajax({
		url : SERVER_URL + '/entity/config',
		method : 'get',
		timeout: 30,
		dataType : 'json',
		cache : false
	}, callback);
};
// 渲染扩展列表
var renderExpandList = function() {
	var $waiters = $api.dom('#waiters');
    $waiters.innerHTML = '';
	WAITERS.some(function(item, i) {

		var waitersArr = [];
		waitersArr.push('<div class="classify" tapmode onclick="openWaiterInfo(\'' + item._id + '\')">');
		waitersArr.push('<img src="' + SERVER_URL + '/entity/file/' + item.photo + '" alt="" class="sublogo">');
		waitersArr.push('<div class="info">');
		waitersArr.push('<div class="title">' + item.name + '</div>');
		waitersArr.push('</div>');
		waitersArr.push('</div>');

		$api.append($waiters, waitersArr.join(''));
	});
};
// 打开服务员信息页面
var openWaiterInfo = function(id) {
	var delay = 0;

	if ("ios" != api.systemType) {
		delay = 300;
	}

	api.openWin({
		name: "waiterinfo",
		url: "./waiter_info.html",
		pageParam: {
			id: id,
            trades: api.pageParam.trades
		},
		bounces: false,
		vScrollBarEnabled: false,
		hScrollBarEnabled: false,
		reload: true,
		delay: delay
	});
}
// 打开服务员查询页面
var openWaiterSearch = function() {
	var delay = 0;

	if ("ios" != api.systemType) {
		delay = 300;
	}

	api.openWin({
		name: "waitersearch",
		url: "./waiter_search.html",
		pageParam: SEARCH,
		bounces: false,
		vScrollBarEnabled: false,
		hScrollBarEnabled: false,
		reload: true,
		delay: delay
	});
};
// 查询服务员
var search = function(params) {
    if (params) {
        SEARCH = params;
    }
    getWaiters(function(waiters, err) {
        if (waiters) {
            WAITERS = waiters.list;
            renderExpandList();
        } else {
            api.alert({msg: '服务端错误。' + err.msg});
        }
    });
};
// 页面加载
var apiready = function() {
    baiduLocation = api.require('baiduLocation');
    /* 一个获取导航栏高度的方法. */
    window.getNavHeight = function() {
        var h = 44;

        if ("ios" == api.systemType && parseInt(api.systemVersion) >= 7) {
            h = 64;
        }

        return h;
    }

    /* 一个关闭窗口,返回上一级页面的优雅实现. */
    window.closeWin = function(winName) {
        api.closeWin();
    }

    // -----------------------------------------
    /* 导航栏自适应iOS 7. */
    var header = $api.byId('header');
    $api.fixIos7Bar(header);

    /* Android,点击返回键,可以返回上一级. */
    window.addEventListener("keyback", function() {
        window.addEventListener("keyback", function() {
            if (!api["pageParam"]["isFrame"]) { // 仅在是主窗口时,才捕捉 keyback事件.
                window.closeWin();
            }
        });
    });

    var trades = api.pageParam.trades;
    if (trades) {
        SEARCH = {
            trades : [trades]
        }
    }
    search();

};
